{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chatbot\n", "\n", "## Try me\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Extra%20Exercises/Ex6%20-%20Chatbot%20(Solved).ipynb)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FExtra%20Exercises%2FEx6%20-%20Chatbot%20(Solved).ipynb)\n", "\n", "Artificial Intelligence (AI) can be defined as the ability of a digital computer to perform tasks normally requiring human intelligence, like visual perception, speech recognition or chatting. A chatbot is a piece of software that may use AI and can interact with humans and keep a basic chat or conversation, very useful in companies to automate interactions with customers. \n", "\n", "Imagine that you run a company that sells digital devices like smartphones or computers, and you want to automate the customer service in your web. Let’s create a chatbot!\n", "\n", "\n", "1. Introduction and welcome.\n", "\n", "Initially, the bot has to ask the user’s name. Then, the user will input his/her name. Since we want a polite bot, the bot must print “Hello, {name}!” being {name} the name the user has input before. For example:\n", "```\n", "Bot:\t- Welcome, what’s your name?\n", "User:\t- Peter\n", "Bot:\t- Hello, Peter!\n", "```\n", "2. Listening to user’s requests.\n", "\n", "The bot will then ask “What do you want to know?” and wait for user input.\n", "\n", "The available commands that user can input are the following:\n", "- “the price of a product”\n", "- “the stock of a product”\n", "- “goodbye”\n", "\n", "Depending on the command, the bot will perform different actions (see Point 3). After the action, the bot must start over asking again “What do you want to know?” and wait for user input.\n", "\n", "This behaviour is repeated in a loop except if the user input is “goodbye”. In this case, the bot will say “Goodbye, {name}!” and exit the program (being {name} the name the user has input in the introduction, see Point 1).\n", "\n", "\n", "3. Replying customer’s inquires.\n", "\n", "If the command input by the user is either “the price of a product” or “the stock of a product”, the bot must ask “Which product?” and wait for user to input one of these products: computer, smartphone, laptop, tablet.\n", "\n", "Depending on the user response, the bot will anser with the required information attending to this table:\n", "```\n", "Products\tPrice\tStock\n", "computer\t1500\t3\n", "smartphone\t900\t7\n", "laptop\t1250\t4\n", "tablet\t400\t5\n", "```\n", "After the action, the bot must start over asking again “What do you want to know?” and wait for user input.\n", "\n", "For example:\n", "```\n", "Bot:\t- What do you want to know?\n", "User:\t- the price of a product\n", "Bot:\t- Which product?\n", "User:\t- smartphone\n", "Bot:\t- The price of a smartphone is 900 euros.\n", "Bot:\t- What do you want to know?\n", "```\n", "Another example:\n", "```\n", "Bot:\t- What do you want to know?\n", "User:\t- the stock of a product\n", "Bot:\t- Which product?\n", "User:\t- tablet\n", "Bot:\t- The stock of the tablet is 5 units.\n", "Bot:\t- What do you want to know?\n", "```\n", "TIP: In order to store the product information, you may use arrays. However, Python dictionaries should be more efficient and neat in this case. For example, for two products:\n", "```\n", "products = {\n", " \"computer\": {\n", " \"price\": 1500,\n", " \"stock\": 3\n", " },\n", " \"smartphone\": {\n", " \"price\": 900,\n", " \"stock\": 7\n", " }\n", "}\n", "```\n", "And for printing the information in the dictionary:\n", "```\n", "print(products[\"computer\"][\"price\"])\n", "print(products[\"smartphone \"][\"stock\"])\n", "```\n", "If you use this example, remember to add laptop and tablet information!\n", "\n", "4. Other cases.\n", "\n", "When the bot asks for user input, if the user input is none of the commands described in Point 2, the bot must answer “Sorry, I don’t understand” and start over again and continue listening to user’s requests by asking “What do you want to know?” and waiting for another user input.\n" ] }, { "cell_type": "markdown", "source": [ "The figure below illustrates the algorithm used in the chatbot:\n", "\n", "![chatbot](img/chatbot.png)\n", "\n", "Note that, to simplify the algorithm, we have introduce two functions, price and stock. The flow diagram of price is included below:\n", "\n", "![chat-bot-price](img/chatbot-Price.png)\n", "\n", "And the flow diagram of the function stock is included below:\n", "\n", "![chat-bot-price](img/chatbot-stock.png)" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "print(\"Welcome, what's your name?\")\n", "name = input()\n", "print(f\"Hello, {name}!\")" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "print(\"Welcome, what's your name?\")\n", "name = input()\n", "print(f\"Hello, {name}!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "print(\"Welcome, what's your name?\")\n", "name = input()\n", "print(f\"Hello, {name}!\")\n", "\n", "while (True):\n", " print(\"What do you want to know?\")\n", " command = input()\n", " if command == \"goodbye\":\n", " print(f\"Goodbye, {name}!\")\n", " break" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "products = {\n", " \"computer\": {\n", " \"price\": 1500,\n", " \"stock\": 3\n", " },\n", " \"smartphone\": {\n", " \"price\": 900,\n", " \"stock\": 7\n", " },\n", " \"laptop\": {\n", " \"price\": 1250,\n", " \"stock\": 4\n", " },\n", " \"tablet\": {\n", " \"price\": 400,\n", " \"stock\": 5\n", " }\n", "}\n", "\n", "print(\"Welcome, what's your name?\")\n", "name = input()\n", "print(f\"Hello, {name}!\")\n", "\n", "while (True):\n", " print(\"What do you want to know?\")\n", " command = input()\n", " if command == \"goodbye\":\n", " print(f\"Goodbye, {name}!\")\n", " break\n", " elif command == \"the price of a product\":\n", " print(\"Which product?\")\n", " product = input()\n", " price = products[product]['price']\n", " print(f\"The price of a {product} is {price} euros.\")\n", " elif command == \"the stock of a product\":\n", " print(\"Which product?\")\n", " product = input()\n", " stock = products[product]['stock']\n", " print(f\"The stock of the {product} is {stock} euros.\")\n", " else:\n", " print(\"Sorry, I don’t understand\")" ] }, { "cell_type": "markdown", "source": [ "## Analysis questions\n", "\n", "1. **Data Structures:** How is the product information (price and stock) stored in the program? Which data structure in Python would be best suited for this task and why?\n", "\n", "2. **Modularity and Extensibility:** How would you handle the addition of more products or more product features to the knowledge of the chatbot?\n", "\n", "3. **Input-Output:** How is interaction with the user implemented? How would you store a conversation between the user and the chatbot? Think of the data structure that would be best suited for this task.\n", "\n", "\n", "4. **Error Handling:** How does the bot handle unrecognized input from the user? What does it do if the user enters a product that's not in its database?\n", "\n", "Accessing Data: Given the provided example using dictionaries, how would you retrieve the stock of a laptop? And the price of a smartphone?\n", "\n", "\n", "\n", "Personalization: How does the chatbot personalize the interaction with the user? How does it remember the user's name throughout the conversation?\n", "\n", "Nested Structures: Describe how the dictionary is structured. Why might nested dictionaries be useful in this scenario?\n", "\n", "Functionality Testing: If you were to test this chatbot's functionality, what are some test cases you would use? Include both typical cases (e.g., asking for the price of a smartphone) and edge cases (e.g., entering a product not on the list)." ], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.4" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 4 }